package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.data.PageBean;
import com.dk.pojo.Car;
import com.dk.service.CarService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("car")
public class CarController {

    @Reference
    private CarService carService;

    @RequestMapping("show")
    public String show(){
        return "car/show";
    }

    //服务端查询
    @RequestMapping("findAll")
    @ResponseBody
    public PageBean<Car> findAll(String cname,Integer ctype,@RequestParam(defaultValue = "1") Integer pageNumber,@RequestParam(defaultValue = "5")Integer pageSize){
        PageBean<Car> car = carService.findAll(cname,ctype,pageNumber,pageSize);
        return car;
    }

    @RequestMapping("add")
    public String add(){
        return "car/add";
    }

    //增加
    @RequestMapping("saveInfo")
    @ResponseBody
    public Boolean saveInfo(Car car){
        Boolean b = carService.saveInfo(car);
        return b;
    }

    //回显
    @RequestMapping("getUpdate")
    public String getUpdate(Integer cid,Model model){
        Car car = carService.getById(cid);
        model.addAttribute("car",car);
        return "car/update";
    }

    //修改
    @RequestMapping("updateInfo")
    @ResponseBody
    public Boolean updateInfo(Car car){
        Boolean b = carService.updateInfo(car);
        return b;
    }

    //删除
    @RequestMapping("deleteInfo")
    @ResponseBody
    public Boolean deleteInfo(Integer cid){
        Boolean b = carService.deleteInfo(cid);
        return b;
    }

    //批量删除
    @RequestMapping("deleteById")
    @ResponseBody
    public Integer deleteById(Integer[] cid){
        return carService.deleteById(cid);
    }

}
